package main

import (
	"context"
	"gameTest/api/middleware"
	"gameTest/api/web/controllers"
	"gameTest/common"
	"gameTest/repositories"
	"gameTest/services"
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"github.com/opentracing/opentracing-go/log"
)

func main()  {
	//创建实例
	app := iris.New()
	//设置错误模式
	app.Logger().SetLevel("debug")

	//读取ini配置
	//_, err := common.NewIni()
	//if err != nil {
	//	log.Error(err)
	//}

	//连接数据库
	db, err := common.NewMysqlConn()
	if err != nil {
		log.Error(err)
	}

	//出现异常信息后显示信息
	app.OnAnyErrorCode(func(ctx iris.Context ) {
		ctx.JSON(common.BuildResult(400, "访问出错",""))
	})

	//创建上下文
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()


	// 注册控制器
	//用户控制器
	userRepository := repositories.NewUserRepository("user", db)
	userService := services.NewUserService(userRepository)
	userParty := app.Party("/user")
	user := mvc.New(userParty)
	user.Register(ctx, userService)
	user.Handle(new(controllers.UserController))

	//产品查询注册
	productRepository := repositories.NewProductManager("product", db)
	productService := services.NewProductService(productRepository)
	productPart := app.Party("/product")
	product := mvc.New(productPart)
	//中间件
	productPart.Use(middleware.AuthConfig)
	product.Register(ctx, productService)
	product.Handle(new(controllers.ProductController))

	//启动服务
	app.Run(
		iris.Addr("127.0.0.1:8080"),
		iris.WithoutServerError(iris.ErrServerClosed),
	)
}
